package com.streamsets.datacollector.pileline.stage;

import com.streamsets.datacollector.model.etl.FieldValueMaskConfigJson;
import com.streamsets.datacollector.restapi.bean.ConfigConfigurationJson;
import com.streamsets.datacollector.restapi.bean.StageConfigurationJson;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  初始化 字段值屏蔽  的Stage的配置
 *  stage的完整描述示例如：
 ""configuration" : [ {
 "name" : "fieldMaskConfigs",
 "value" : [ {
 "fields" : [ "/remark", "/school_org_id" ],
 "maskType" : "VARIABLE_LENGTH",
 "regex" : "(.*)",
 "groupsToShow" : "1"
 } ]
 },....]
 *
 * @author wengzhonghui
 * @date 2020/9/16 10:32
 */
public class InitFieldValueMaskConfig {

    /*
     * stage instanceName名称
     */
    private static final String STAGE_NAME = "g1_03";

    /*
     * 名称
     */
    private static final String CONFIG_NAME_MASK = "fieldMaskConfigs";


    public static boolean init(StageConfigurationJson stage, FieldValueMaskConfigJson configDto){
        if(STAGE_NAME.equalsIgnoreCase(stage.getInstanceName())){
            if (configDto != null) {
                List<ConfigConfigurationJson> configs = intConfig(stage.getConfiguration(), configDto);
                stage.setConfiguration(configs);
            } else {
                List<ConfigConfigurationJson> configs =
                    stage.getConfiguration().stream().filter(e -> "fieldMaskConfigs".equals(e.getName())).collect(Collectors
                        .toList());
                stage.getConfiguration().removeAll(configs);
            }
        }
        return true;
    }

    public static List<ConfigConfigurationJson> intConfig(List<ConfigConfigurationJson> oldConfigs, FieldValueMaskConfigJson configDto){
        List<ConfigConfigurationJson> newConfigs = new ArrayList<>();
        for(ConfigConfigurationJson config :oldConfigs ){
            config = initFieldValueMaskConfigs(config, configDto);
            newConfigs.add(config);
        }
        return newConfigs;
    }


    /*
    * 把 列值屏蔽
    * 格式示例如：
    *[ {
          "fields" : [ "/remark", "/school_org_id" ],
          "maskType" : "VARIABLE_LENGTH",
          "regex" : "(.*)",
          "groupsToShow" : "1"
        } ]
    * @param [config 原来的配置, columnNames:需要替换为null的列名列表]
    * @return 返回新生成的配置
    */
    private static ConfigConfigurationJson initFieldValueMaskConfigs(ConfigConfigurationJson config, FieldValueMaskConfigJson configDto){
        if(CONFIG_NAME_MASK.equalsIgnoreCase(config.getName())){
            List configValList = new ArrayList();
            if(null != configDto.getConfig() && !configDto.getConfig().isEmpty()) {
                configDto.getConfig().forEach(dto ->{
                    if (configDto != null && dto.getFieldReplaceColumns() != null
                        && dto.getFieldReplaceColumns().size() > 0) {
                        List<String> sourceFields = dto.getFieldReplaceColumns();
                        List<String> targetFields = new ArrayList<>();
                        for (String field : sourceFields) {
                            targetFields.add("/" + field);
                        }
                        String regex = StringUtils.isEmpty(dto.getRegex()) ? "(.*)" : dto.getRegex();
                        Map<String, Object> data = new HashMap();
                        data.put("fields", targetFields);
                        data.put("maskType", dto.getMaskType());
                        data.put("regex", regex);
                        data.put("mask", dto.getMask());
                        data.put("groupsToShow", dto.getGroupsToShow());
                        configValList.add(data);
                    }
                });

                config = new ConfigConfigurationJson(CONFIG_NAME_MASK, configValList);
            }
        }
        return config;
    }


}
